{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import dependencies and modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Libraries\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import h5py\n",
    "import xarray as xr\n",
    "import glob as glob\n",
    "import sys\n",
    "import os.path\n",
    "import os.listdir\n",
    "\n",
    "# Our modules\n",
    "sys.path.append('/home/jovyan/segtrax/source')\n",
    "import utilities as ut\n",
    "import reader as rd\n",
    "from trajectory import Trajectory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ERA-I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'os' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-65927d5ea341>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/home/jovyan/segtrax/data/era_i/'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Downloading ERA-I data to era_i/ directory\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2018-11.nc ../data/era_i/era-interim-t2m.2018-11.nc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2018-12.nc ../data/era_i/era-interim-t2m.2018-12.nc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'os' is not defined"
     ]
    }
   ],
   "source": [
    "if len(os.listdir('/home/jovyan/segtrax/data/era_i/')) <= 1:\n",
    "    print(\"Downloading ERA-I data to era_i/ directory\")\n",
    "    \n",
    "    !aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2018-11.nc ../data/era_i/era-interim-t2m.2018-11.nc\n",
    "    !aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2018-12.nc ../data/era_i/era-interim-t2m.2018-12.nc\n",
    "    !aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2019-01.nc ../data/era_i/era-interim-t2m.2019-01.nc\n",
    "    !aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/era-interim-t2m.2019-02.nc ../data/era_i/era-interim-t2m.2019-02.nc\n",
    "    \n",
    "else:\n",
    "    print(\"Already have ERA-I data\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ATL10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading ATL10 data to atl10/ directory\n",
      "download: s3://pangeo-data-upload-oregon/icesat2/segtrax/Outputs.tar.gz to ../data/atl10/Outputs.tar.gz\n"
     ]
    }
   ],
   "source": [
    "if len(os.listdir('/home/jovyan/segtrax/data/atl10/')) <= 1:\n",
    "    print(\"Downloading ATL10 data to atl10/ directory\")\n",
    "\n",
    "    !aws s3 cp s3://pangeo-data-upload-oregon/icesat2/segtrax/Outputs.tar.gz /home/jovyan/segtrax/data/atl10/\n",
    "    !tar -xzvf /home/jovyan/segtrax/data/atl10/Outputs.tar.gz -C /home/jovyan/segtrax/data/atl10/ --strip-components=12\n",
    "\n",
    "else:\n",
    "    print(\"Already have ATL10 data\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Ice Motion\n",
    "- Included in `data/` directory in repo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize list of objects\n",
    "1. Get list of data files with glob library\n",
    "2. Iterate through each file and create a trajectory object with that file's data. Add the object to a list.\n",
    "\n",
    "Potential improvements: simplify .h5 files and use parallelization stuff from Fernando's tutorial?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161109914/processed_ATL10-01_20181105062227_05750101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161053169/processed_ATL10-01_20181102153049_05350101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161184155/processed_ATL10-01_20181101190501_05220101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161184173/processed_ATL10-01_20181102201341_05380101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161185873/processed_ATL10-01_20181106165654_05970101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161156884/processed_ATL10-01_20181103040509_05430101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161182786/processed_ATL10-01_20181101155626_05200101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161187844/processed_ATL10-01_20181101142209_05190101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161156880/processed_ATL10-01_20181103212220_05540101_001_01.h5\n",
      "ATL10 file: /home/jovyan/segtrax/data/ATL10_tracks/161184744/processed_ATL10-01_20181104143933_05650101_001_01.h5\n"
     ]
    }
   ],
   "source": [
    "directory = '/home/jovyan/segtrax/data/ATL10_tracks/*/*'\n",
    "files = glob.glob(directory)\n",
    "\n",
    "traj_list = []\n",
    "\n",
    "for f in files[0:10]:\n",
    "    \n",
    "    segment_id, segment_length, date, lons, lats, freeboard = rd.getATL10(f, beam='gt1l')\n",
    "    \n",
    "    for sid, slen, d, la, lo, fb in zip(segment_id, segment_length, date, lats, lons, freeboard):\n",
    "        traj_list.append(Trajectory(sid, slen, d, la, lo, fb))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Trajectory for segment 1 with length 8259610.951683317"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "traj_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3340717"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(traj_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
